package cn.itcast.mq.listen;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;

import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Map;

@Component
public class SpringRabbitListener {

//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueueMessage(String msg){
//        System.out.println("消费者接收到消息:"+msg);
//    }

    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue1Message(String msg) throws InterruptedException {
        System.out.println("消费者1接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(20);
    }
    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue2Message(String msg) throws InterruptedException {
        System.err.println("消费者2........接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1Message(String msg){
        System.out.println("消费者1接收到消息：【" + msg + "】" + LocalTime.now());
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2Message(String msg){
        System.out.println("消费者2接收到消息：【" + msg + "】" + LocalTime.now());
    }

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue("direct.queue1"),
//            exchange = @Exchange("itcast.direct"),
//            key = {"red","blue"}
//    ))
//    public void listenDirectQueue1Message(String msg){
//        System.out.println("消费者1接收到消息：【" + msg + "】" + LocalTime.now());
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "direct.queue2"),
//            exchange = @Exchange(value = "itcast.direct"),
//            key = {"red","yellow"}
//    ))
//    public void listenDirectQueue2Message(String msg){
//        System.out.println("消费者2接收到消息：【" + msg + "】" + LocalTime.now());
//    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "topic.queue1"),
            exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue1Message(String msg){
        System.out.println("消费者1接收到消息：【" + msg + "】" + LocalTime.now());
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "topic.queue2"),
            exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenTopicQueue2Message(String msg){
        System.out.println("消费者2接收到消息：【" + msg + "】" + LocalTime.now());
    }

    @RabbitListener(queues = "simple.queue")
    public void testObjectMessage(Map map){
        System.out.println(map);
    }
}
